package cn.b.sky.mes.em.controller;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.em.model.condition.ProductionResultsCondition;
import cn.b.sky.mes.em.service.ProductionResultsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.em.model.EquipmentDownTime;
import cn.b.sky.mes.em.model.ProductionResults;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping(value = "/emOee/*")
public class OeeController extends SecurityController {
  @Resource
  ProductionResultsService productionResultsService;

  @RequestMapping("emOeeList")
  public String emOeeList(Model model, @ModelAttribute("condition") ProductionResultsCondition condition,
      String navTabId) {
    if (condition.getTheDateEd() == null) {
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      Calendar c = Calendar.getInstance();
      c.add(Calendar.DATE, -1);
      Date now = df.parse(df.format(c.getTime()), new ParsePosition(0));
      condition.setTheDateEd(now);
      condition.setTheDateSt(now);
    }
    condition.setSiteCode(SecurityContextUtil.getSiteCode());
    List<ProductionResults> results = productionResultsService.selectProductionResultsForOee(condition);
    int count = productionResultsService.selectProductionResultsForOeeCount(condition);
    condition.setData(results);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "em/oee/oee_chart";
  }

  @RequestMapping(value = "getShutDownCause")
  @ResponseBody
  public DWZResult getShutDownCause(String equipmentCode, String theDateSt, String theDateEd) {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date theDateStDay = df.parse(theDateSt, new ParsePosition(0));
    Date theDateEdDay = df.parse(theDateEd, new ParsePosition(0));
    ProductionResultsCondition condition = new ProductionResultsCondition();
    condition.setSiteCode(getSiteCode());
    condition.setEquipmentCode(equipmentCode);
    condition.setTheDateSt(theDateStDay);
    condition.setTheDateEd(theDateEdDay);
    List<EquipmentDownTime> details = productionResultsService.selectShutDownCauseForOee(condition);
    String orderDetailsJSON = JSON.toJSONString(details);
    return ajaxDone(orderDetailsJSON);
  }

  @RequestMapping(value = "getShutDownDetail")
  @ResponseBody
  public DWZResult getShutDownDetail(String equipmentCode, String theDateSt, String theDateEd) {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date theDateStDay = df.parse(theDateSt, new ParsePosition(0));
    Date theDateEdDay = df.parse(theDateEd, new ParsePosition(0));
    ProductionResultsCondition condition = new ProductionResultsCondition();
    condition.setSiteCode(getSiteCode());
    condition.setEquipmentCode(equipmentCode);
    condition.setTheDateSt(theDateStDay);
    condition.setTheDateEd(theDateEdDay);
    List<EquipmentDownTime> details = productionResultsService.selectShutDownListForOee(condition);
    String orderDetailsJSON = JSON.toJSONString(details);
    return ajaxDone(orderDetailsJSON);
  }

  @RequestMapping("emOeeTrend")
  public String emOeeTrend(Model model, @ModelAttribute("condition") ProductionResultsCondition condition,
      String navTabId) {
    if (condition.getTheDateEd() == null) {
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      Calendar c = Calendar.getInstance();
      c.add(Calendar.DATE, -1);
      Date end = df.parse(df.format(c.getTime()), new ParsePosition(0));
      c.add(Calendar.DATE, -6);
      Date start = df.parse(df.format(c.getTime()), new ParsePosition(0));
      condition.setTheDateEd(end);
      condition.setTheDateSt(start);
    }
    condition.setSiteCode(SecurityContextUtil.getSiteCode());
    List<ProductionResults> results = productionResultsService.selectProductionResultsForOee(condition);
    int count = productionResultsService.selectProductionResultsForOeeCount(condition);
    condition.setData(results);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "em/oee/oee_trend";
  }

  @RequestMapping(value = "getOeeTrend")
  @ResponseBody
  public DWZResult getOeeTrend(String equipmentCode, String theDateSt, String theDateEd) {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date theDateStDay = df.parse(theDateSt, new ParsePosition(0));
    Date theDateEdDay = df.parse(theDateEd, new ParsePosition(0));
    ProductionResultsCondition condition = new ProductionResultsCondition();
    condition.setSiteCode(getSiteCode());
    condition.setEquipmentCode(equipmentCode);
    condition.setTheDateSt(theDateStDay);
    condition.setTheDateEd(theDateEdDay);
    List<ProductionResults> details = productionResultsService.selectProductionResultsForTrend(condition);
    String orderDetailsJSON = JSON.toJSONString(details);
    return ajaxDone(orderDetailsJSON);
  }
}
